package com.wanpinghui.wphlibrary.model.object;

import com.wanpinghui.wphlibrary.common.IWPHApi;
import com.wanpinghui.wphlibrary.model.base.bean.ImageBean;
import com.wanpinghui.wphlibrary.model.object.bean.BidObjectResult;
import com.wanpinghui.wphlibrary.model.object.bean.DemandBean;
import com.wanpinghui.wphlibrary.model.object.bean.FeedBackReasonBean;
import com.wanpinghui.wphlibrary.model.object.bean.FeedbackInfoBean;
import com.wanpinghui.wphlibrary.model.object.bean.GetOjbectDetailResult;
import com.wanpinghui.wphlibrary.model.object.bean.GetOjbectListResult;
import com.wanpinghui.wphlibrary.model.object.bean.GetParticipationListResult;
import com.wanpinghui.wphlibrary.model.object.bean.ObjectDetailBean;
import com.yunshl.yunshllibrary.utils.TextUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit.Network;
import retrofit.callback.YRequestCallback;
import retrofit.callback.YunShlAction;
import retrofit.callback.YunShlResult;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/***
 * 功能描述:
 * 作者:qiujialiu
 * 时间:2017/11/23
 ***/

public class ObjectServiceImp implements ObjectService {

    @Override
    public void getObjectList(long sortVal, String city, String business, String demand, final YRequestCallback<List<ObjectDetailBean>> callback) {
        Network.getApi(IWPHApi.class).getObjectList(sortVal==0?null:sortVal,
                city,business,demand,20).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<YunShlResult<GetOjbectListResult>>() {
                    @Override
                    public void call(YunShlResult<GetOjbectListResult> result) {
                        if (result.isSuccess()) {
                            if (callback != null) {
                                callback.onSuccess(result.body.getList());
                            }
                        }else {
                            if (callback != null) {
                                callback.onFailed(0,result.msg);
                            }
                        }
                    }
                }, new YunShlAction<Throwable>(callback));
    }

    @Override
    public void getObjectDetail(long demandId,final YRequestCallback<ObjectDetailBean> callback) {
        Network.getApi(IWPHApi.class).getObjectDetail(demandId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<YunShlResult<GetOjbectDetailResult>>() {
                    @Override
                    public void call(YunShlResult<GetOjbectDetailResult> result) {
                        if (result.isSuccess()) {
                            if (callback != null) {
                                callback.onSuccess(result.body.getInfo());
                            }
                        }else {
                            if (callback != null) {
                                if (result.status == 41004002) {
                                    callback.onFailed(41004002, "项目不存在");
                                }else {
                                    callback.onFailed(0, result.msg);
                                }
                            }
                        }
                    }
                }, new YunShlAction<Throwable>(callback));
    }

    @Override
    public void getParticList(long demandId, final YRequestCallback<GetParticipationListResult> callback) {
        Network.getApi(IWPHApi.class).getParticList(demandId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<YunShlResult<GetParticipationListResult>>() {
                    @Override
                    public void call(YunShlResult<GetParticipationListResult> result) {
                        if (result.isSuccess()) {
                            if (callback != null) {
                                callback.onSuccess(result.body);
                            }
                        }else {
                            if (callback != null) {
                                callback.onFailed(0, result.msg);
                            }
                        }
                    }
                }, new YunShlAction<Throwable>(callback));
    }

    @Override
    public void bidObject(long demandId, int price, final YRequestCallback<ObjectDetailBean> callback) {
        Map<String,Object> params = new HashMap<>();
        params.put("offer", price);
        Network.getApi(IWPHApi.class).bidObject(demandId,params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<YunShlResult<BidObjectResult>>() {
                    @Override
                    public void call(YunShlResult<BidObjectResult> result) {
                        if (result.isSuccess()) {
                            if (callback != null) {
                                callback.onSuccess(result.body.getDemand_info());
                            }
                        }else {
                            if (callback != null) {
                                callback.onFailed(result.status, result.msg);
                            }
                        }
                    }
                }, new YunShlAction<Throwable>(callback));
    }

    @Override
    public void callPhone(String caller, String called, final YRequestCallback callback) {
        Map<String,Object> params = new HashMap<>();
        params.put("caller", caller);
        params.put("called", called);
        Network.getApi(IWPHApi.class).callNumber(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<YunShlResult<Object>>() {
                    @Override
                    public void call(YunShlResult<Object> result) {
                        if (result.isSuccess()) {
                            if (callback != null) {
                                callback.onSuccess(result.body);
                            }
                        }else {
                            if (callback != null) {
                                callback.onFailed(result.status, result.msg);
                            }
                        }
                    }
                }, new YunShlAction<Throwable>(callback));
    }

    @Override
    public void objectReport(long demandId, String photoBase64, final YRequestCallback<ImageBean> callback) {
        if (TextUtil.isEmpty(photoBase64)) {
            if (callback != null) {
                callback.onFailed(0,"获取图片资源失败");
            }
        }
        Map<String,Object> params = new HashMap<>();
        params.put("img", photoBase64);
        Network.getApi(IWPHApi.class).objectReport(demandId,params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<YunShlResult<ImageBean>>() {
                    @Override
                    public void call(YunShlResult<ImageBean> result) {
                        if (result.isSuccess()) {
                            if (callback != null) {
                                callback.onSuccess(result.body);
                            }
                        }else {
                            if (callback != null) {
                                callback.onFailed(result.status, result.msg);
                            }
                        }
                    }
                }, new YunShlAction<Throwable>(callback));
    }

    @Override
    public void objectFeedback(long demandId, int type,String content ,final YRequestCallback<Object> callback) {
        Map<String,Object> params = new HashMap<>();
        params.put("feedback", type);
        if (TextUtil.isNotEmpty(content)) {
            params.put("feedback_content", content);
        }
        Network.getApi(IWPHApi.class).objectFeedback(demandId, params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<YunShlResult<FeedbackInfoBean>>() {
                    @Override
                    public void call(YunShlResult<FeedbackInfoBean> result) {
                        if (result.isSuccess()) {
                            if (callback != null) {
                                callback.onSuccess(result.body);
                            }
                        }else {
                            if (callback != null) {
                                callback.onFailed(result.status, result.msg);
                            }
                        }
                    }
                }, new YunShlAction<Throwable>(callback));
    }

    @Override
    public void objectFeedback(long demandId, int type, int feedbackType, final YRequestCallback<FeedbackInfoBean> callback) {
        Map<String,Object> params = new HashMap<>();
        if (type != 0) {
            params.put("feedback", type);
        }
        params.put("feedback_type", feedbackType);
        Network.getApi(IWPHApi.class).objectFeedback(demandId, params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<YunShlResult<FeedbackInfoBean>>() {
                    @Override
                    public void call(YunShlResult<FeedbackInfoBean> result) {
                        if (result.isSuccess()) {
                            if (callback != null) {
                                callback.onSuccess(result.body);
                            }
                        }else {
                            if (callback != null) {
                                callback.onFailed(result.status, result.msg);
                            }
                        }
                    }
                }, new YunShlAction<Throwable>(callback));
    }

    @Override
    public void getFeedBackType(final YRequestCallback<List<FeedBackReasonBean>> callback) {
        Network.getApi(IWPHApi.class).getFeedBackTypes()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<YunShlResult<List<FeedBackReasonBean>>>() {
                    @Override
                    public void call(YunShlResult<List<FeedBackReasonBean>> result) {
                        if (result.isSuccess()) {
                            if (callback != null) {
                                callback.onSuccess(result.body);
                            }
                        }else {
                            if (callback != null) {
                                callback.onFailed(result.status, result.msg);
                            }
                        }
                    }
                }, new YunShlAction<Throwable>(callback));
    }
}
